<?php

namespace backend\controllers;

use Yii;
use backend\models\City;
use backend\models\CitySearch;
use backend\base\BaseBackendController;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

use base\YiiMobo;
/**
 * CityController implements the CRUD actions for City model.
 */
class CityController extends BaseBackendController
{
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }

    /**
     * Lists all City models.
     * @return mixed
     */
    public function actionIndex()
    {
        $param['sql'] = 'select * from {{%city}}';
        $param['order'] = '';
        $param['param']['search'] = isset($_REQUEST['search']) ? $_REQUEST['search'] : '';
		$param['render'] = 'index';
		self::pageView($param);
    }

    /**
     * Displays a single City model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new City model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $this->CreateUpdate();
    }

    /**
     * Updates an existing City model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $this->CreateUpdate($id);
    }
    
    
	/**
	 * 添加修改数据内容.
     * 添加数据库内容的表单提交处理
	 * 模板文件通常为  _form.php
	 */
	public function CreateUpdate($id = null)
	{
		$model = self::findModel($id);
		if ($model->load(Yii::$app->request->post()))
		{
			if($model->save())
            {
			    $this->OperTips('/city/index');
			}
		}
     	return $this->render('_form',[
			'model'=>$model,
		]);
	}
    /**
	 * 批量操作（删除）一些记录
	 * 一般来自控制器的默认方法index
	 * @param $id 一般为数据库表的主键值
	 */
	public function actionBatch()
	{
        $ids = isset($_POST['checked_id']) ? $_POST['checked_id'] : NULL;
		$tips = '';
		$backurl = $_SERVER['HTTP_REFERER'];
        if ($ids){
            if (isset($_POST['batch_delete'])){
                City::find()->where(['deleted' => false])->all()->delete();
            }
        }else if(isset($_POST['batch_listorder'])){
			if ($_POST['listorders']) foreach($_POST['listorders'] as $id=>$listorder){
				City::updateAll(['listorder'=>intval($listorder)], 'menuid='.$id);
			}
		}
        self::actionCache();
		$this->OperTips('/city/index');
	}
    /**
     * Deletes an existing City model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();
        $this->OperTips();
    }

    /**
     * Finds the City model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return City the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id = null)
    {
        $model = $id ? City::findOne($id) : new City;
        if ($model !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
